const cart = {
  namespaced: true,
  // 因为所有模块的方法都注册在全局模块中，所以需要创建一个命名空间，使得本模块的方法注册在本模块中
  state: {
    carts: {
      totalPrice: 0,
      isAllchecked: true,
      items: [
        /*
        afterSale: "0,1,2"
        categoryId: 164510
        characteristic: "粉/蓝双色人造纸7张，粉色缎带1米"
        commission: 0
        commissionType: 0
        dateAdd: "2020-09-09 20:33:16"
        dateUpdate: "2020-09-12 16:24:40"
        fxType: 2
        gotScore: 0
        gotScoreType: 0
        hidden: 0
        id: 565048
        isChecked: true
        kanjia: false
        kanjiaPrice: 0
        limitation: false
        logisticsId: 0
        maxCoupons: 1
        miaosha: false
        minBuyNumber: 1
        minPrice: 199
        minScore: 0
        name: "钟意你"
        num: 1
        numberFav: 0
        numberGoodReputation: 0
        numberOrders: 0
        numberSells: 0
        originalPrice: 259
        overseas: false
        paixu: 0
        pic: "https://dcdn.it120.cc/2020/09/09/bdc5d13e-fd0b-4dbb-8ca6-c79bbc12cecc.jpg"
        pingtuan: false
        pingtuanPrice: 0
        purchaseNotes: "因季节原因和各地花艺师包扎手法不同，实物的辅材和细节可能与图片不完全一致，我们保证主花材与说明一致，谢谢您的理解！"
        recommendStatus: 0
        recommendStatusStr: "普通"
        sellEnd: false
        sellStart: true
        shopId: 0
        status: 0
        statusStr: "上架"
        storeAlert: false
        storeAlertNum: 1
        stores: 7
        stores0Unsale: false
        tags: "戴安娜玫瑰11枝,粉色太阳花9枝,大叶尤加利6枝"
        type: 0
        unit: "份"
        userId: 29026
        vetStatus: 1
        views: 17
        weight: 0.5
        */
      ]
    }
  },
  mutations: {
    // 计算总价格
    calcTotal (state, total) {
      state.carts.totalPrice = total
    },
    // 全选按钮点击设置全选、取消全选
    setAllCheckedState (state, allCheckedState) {
      state.carts.items.forEach(item => { item.isChecked = allCheckedState })
    },
    // 删除商品
    delItem (state, index) {
      state.carts.items.splice(index, 1)
    },
    // 商品数量改变
    toChange (state, carts) {
      state.carts = carts
    },
    // 单选事件改变全选状态
    setSingleState (state, allChecked) {
      state.carts.isAllchecked = allChecked
    },
    // 加入购物车
    addCart (state, item) {
      state.carts.items.push(item)
    },
    // 增加购买数量
    addItemNum (state, index) {
      state.carts.items[index].num++
    }
  },
  actions: {
    // 加入购物车
    toAddCart ({ commit, state: { carts } }, item) {
      const { items } = carts
      const index = items.findIndex(el => el.id === item.id)
      if (index === -1) {
        commit('addCart', item)
      } else {
        commit('addItemNum', index)
      }
    },
    // 计算总价格
    calc_total ({ commit, state: { carts } }) {
      const { items } = carts
      let total = 0
      // 过滤已选中的商品
      const checkedItems = items.filter(item => item.isChecked)
      // 计算总价格
      checkedItems.forEach(el => {
        total += el.num * el.minPrice
      })
      total = parseFloat(total.toFixed(2))
      commit('calcTotal', total)
    }
  },
  modules: {
  }

}

export default cart
